//
//  NSObject+TYUIKitSwizzler.m
//  TYUIKit
//
//  Created by TuyaInc on 2019/3/13.
//

#import "NSObject+TYUIKitSwizzler.h"

#import <objc/runtime.h>

@implementation NSObject (TYUIKitSwizzler)

+ (void)ty_uikit_swizzleSEL:(SEL)oriSel withSEL:(SEL)swiSel {
    [self ty_uikit_swizzleSEL:oriSel withSEL:swiSel fromClass:self];
}

+ (void)ty_uikit_swizzleSEL:(SEL)oriSel withSEL:(SEL)swiSel fromClass:(Class)implCls {
    Method oriMethod = class_getInstanceMethod(self, oriSel);
    Method swiMethod = class_getInstanceMethod(implCls ? : self, swiSel);
    if (!oriMethod || !swiMethod) return;
    
    class_addMethod(self,
                    oriSel,
                    class_getMethodImplementation(self, oriSel),
                    method_getTypeEncoding(oriMethod));
    class_addMethod(self,
                    swiSel,
                    class_getMethodImplementation(implCls ? : self, swiSel),
                    method_getTypeEncoding(swiMethod));
    
    method_exchangeImplementations(class_getInstanceMethod(self, oriSel),
                                   class_getInstanceMethod(self, swiSel));
}


@end
